extends Control
class_name HealthDisplay

# 玩家健康状态显示组件
# 在HUD左上角显示血条和可能的魔法值

@export var max_health: int = 100
@export var current_health: int = 100

# UI节点
var health_bar_bg: NinePatchRect
var health_bar_fill: NinePatchRect
var health_text: Label

# 初始化标志
var is_initialized: bool = false

# 血条配置 - 调整为更适合小屏幕的尺寸
const BAR_WIDTH = 70  # 稍微缩小
const BAR_HEIGHT = 10  # 稍微缩小

func _ready():
	if not is_initialized:
		initialize()

func initialize():
	if is_initialized:
		print("HealthDisplay 已经初始化过了，跳过重复初始化")
		return
		
	print("HealthDisplay initialize() 开始...")
	
	# 设置控件尺寸 - 不覆盖场景中的位置设置，增加高度以容纳标题
	size = Vector2(BAR_WIDTH + 10, BAR_HEIGHT + 17)
	
	# 只在没有设置位置时才设置默认位置
	if position == Vector2.ZERO:
		anchors_preset = Control.PRESET_TOP_LEFT
		position = Vector2(10, 10)
	
	create_health_bar()
	update_display()
	
	is_initialized = true
	print("HealthDisplay 初始化完成，尺寸: ", size, " 位置: ", position)

func create_health_bar():
	# 创建健康状态标题
	var health_title = Label.new()
	add_child(health_title)
	health_title.text = "生命值"
	health_title.position = Vector2(5, -12)
	health_title.size = Vector2(BAR_WIDTH, 10)
	health_title.add_theme_color_override("font_color", Color.RED)
	health_title.add_theme_color_override("font_shadow_color", Color.BLACK)
	health_title.add_theme_constant_override("shadow_offset_x", 1)
	health_title.add_theme_constant_override("shadow_offset_y", 1)
	health_title.add_theme_font_size_override("font_size", 8)
	
	# 创建抗锯齿字体
	var title_font = SystemFont.new()
	title_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	title_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	title_font.hinting = TextServer.HINTING_LIGHT
	health_title.add_theme_font_override("font", title_font)
	
	# 创建血条背景
	health_bar_bg = NinePatchRect.new()
	add_child(health_bar_bg)
	health_bar_bg.texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/ui (new)/health_ui.png")
	health_bar_bg.size = Vector2(BAR_WIDTH, BAR_HEIGHT)
	health_bar_bg.position = Vector2(5, 2)
	health_bar_bg.modulate = Color(0.3, 0.3, 0.3, 0.8)
	
	# 创建血条填充
	health_bar_fill = NinePatchRect.new()
	add_child(health_bar_fill)
	health_bar_fill.texture = load("res://Art/v1.1 dungeon crawler 16X16 pixel pack/ui (new)/health_ui.png")
	health_bar_fill.size = Vector2(BAR_WIDTH, BAR_HEIGHT)
	health_bar_fill.position = Vector2(5, 2)
	health_bar_fill.modulate = Color.GREEN
	
	# 创建血量文字 - 调整字体大小
	health_text = Label.new()
	add_child(health_text)
	health_text.size = Vector2(BAR_WIDTH, BAR_HEIGHT)
	health_text.position = Vector2(5, 2)
	health_text.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	health_text.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	health_text.add_theme_color_override("font_color", Color.WHITE)
	health_text.add_theme_color_override("font_shadow_color", Color.BLACK)
	health_text.add_theme_constant_override("shadow_offset_x", 1)
	health_text.add_theme_constant_override("shadow_offset_y", 1)
	
	# 设置适合的字体大小
	var font_size = 10  # 增大字体以提高清晰度
	health_text.add_theme_font_size_override("font_size", font_size)
	
	# 创建抗锯齿字体
	var text_font = SystemFont.new()
	text_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	text_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	text_font.hinting = TextServer.HINTING_LIGHT
	health_text.add_theme_font_override("font", text_font)

func update_health(new_current: int, new_max: int):
	current_health = clamp(new_current, 0, new_max)
	max_health = new_max
	update_display()

func update_display():
	if not health_bar_fill or not health_text:
		return
	
	# 计算血量百分比
	var health_ratio = float(current_health) / float(max_health) if max_health > 0 else 0.0
	
	# 更新血条宽度
	health_bar_fill.size.x = BAR_WIDTH * health_ratio
	
	# 更新血条颜色
	if health_ratio > 0.6:
		health_bar_fill.modulate = Color.GREEN
	elif health_ratio > 0.3:
		health_bar_fill.modulate = Color.YELLOW
	else:
		health_bar_fill.modulate = Color.RED
	
	# 更新文字 - 简化显示
	health_text.text = str(current_health) + "/" + str(max_health)

func animate_damage():
	# 受伤时的动画效果
	if health_bar_fill:
		var tween = create_tween()
		tween.tween_property(health_bar_fill, "modulate", Color.WHITE, 0.1)
		tween.tween_property(health_bar_fill, "modulate", get_health_color(), 0.1)

func get_health_color() -> Color:
	var health_ratio = float(current_health) / float(max_health) if max_health > 0 else 0.0
	
	if health_ratio > 0.6:
		return Color.GREEN
	elif health_ratio > 0.3:
		return Color.YELLOW
	else:
		return Color.RED 